//
//  LoginViewController.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/9.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "JTNavigationController.h"
#import "WXApiManager.h"
#import "WechatAuthSDK.h"

#import "ModifyLoginPasswordViewController.h"
#import "NSData+Base64.h"

#define WX_BASE_URL     @"https://api.weixin.qq.com/sns"

#define WXAccess_token  @"WXAccess_token"
#define WXOpenID        @"WXOpenID"
#define WXRefresh_token @"WXRefresh_token"

@interface LoginViewController () <WXApiManagerDelegate>

@property (strong, nonatomic) UIButton *closeBtn;

@property (strong, nonatomic) UIScrollView *contentScrollView;

@property (strong, nonatomic) UIImageView *titileIV;
@property (strong, nonatomic) UIImageView *leftIV;
@property (strong, nonatomic) UIImageView *rightIV;
@property (strong, nonatomic) UILabel *desL;

@property (strong, nonatomic) UIImageView *phoneIV;
@property (strong, nonatomic) UILabel *phoneL;
@property (strong, nonatomic) UILabel *phoneZoneL;
@property (strong, nonatomic) UITextField *phoneTextField;
@property (strong, nonatomic) UIView *shortLineIV;
@property (strong, nonatomic) UIView *middleLineIV;

@property (strong, nonatomic) UIImageView *passwordIV;
@property (strong, nonatomic) UILabel *passwordL;
@property (strong, nonatomic) UITextField *passwordTextField;
@property (strong, nonatomic) UIView *longLineIV;

@property (strong, nonatomic) UIButton *goReisterBtn;
@property (strong, nonatomic) UIButton *forgetPasswrodBtn;

@property (strong, nonatomic) UIView *otherLeftV;
@property (strong, nonatomic) UIView *otherRightV;
@property (strong, nonatomic) UILabel *otherLoginL;

@property (strong, nonatomic) UIButton *wxBtn;
@property (strong, nonatomic) UIButton *tbBtn;


@property (strong, nonatomic) UIButton *loginBtn;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WhiteColor;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.closeBtn.frame = CGRectMake(0, 0, jisuan(18), jisuan(32));
    
    UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithCustomView:self.closeBtn];
    
    self.navigationItem.rightBarButtonItem = btnItem;

    [self createView];
    
    [WXApiManager sharedManager].delegate = self;
    
}

#pragma mark -微信登录
- (void)wxBtnClick:(UIButton *)btn {
    
    NSLog(@"微信授权登录");

    NSString *access_token = [[NSUserDefaults standardUserDefaults] valueForKey:WXAccess_token];
    NSString *openID = [[NSUserDefaults standardUserDefaults] valueForKey:WxAppID];

    // 如果已经请求过微信授权登录，那么考虑用已经得到的access_token
    if (access_token && openID) {

        NSString *refresh_token = [[NSUserDefaults standardUserDefaults] valueForKey:WXRefresh_token];

        NSString *refreshUrlStr = [NSString stringWithFormat:@"%@/oauth2/refresh_token?appid=%@&grant_type=refresh_token&refresh_token=%@", WX_BASE_URL, WxAppID, refresh_token];

        [PPNetworkHelper POST:refreshUrlStr parameters:nil success:^(id responseObject) {

            NSLog(@"请求reAccess的response = %@", responseObject);
            NSDictionary *refreshDict = [NSDictionary dictionaryWithDictionary:responseObject];
            NSString *reAccessToken = [refreshDict objectForKey:WXAccess_token];
            // 如果reAccessToken为空,说明reAccessToken也过期了,反之则没有过期
            if (reAccessToken) {
                // 更新access_token、refresh_token、open_id
                [[NSUserDefaults standardUserDefaults] setObject:reAccessToken forKey:WXAccess_token];
                [[NSUserDefaults standardUserDefaults] setObject:[refreshDict objectForKey:WXOpenID] forKey:WXOpenID];
                [[NSUserDefaults standardUserDefaults] setObject:[refreshDict objectForKey:WXRefresh_token] forKey:WXRefresh_token];
                [[NSUserDefaults standardUserDefaults] synchronize];
                // 当存在reAccessToken不为空时直接执行AppDelegate中的wechatLoginByRequestForUserInfo方法
//                !self.requestForUserInfoBlock ? : self.requestForUserInfoBlock();

                [self wechatLoginByRequestForUserInfo];

            }else {

                [self sendAuthRequest];

            }

        } failure:^(NSError *error) {

               NSLog(@"用refresh_token来更新accessToken时出错 = %@", error);

        }];

    }else {

        [self sendAuthRequest];

    }

}

- (void)sendAuthRequest {
    
    if ([WXApi isWXAppInstalled]) {

        SendAuthReq *req = [[SendAuthReq alloc] init];
//        req.scope = @"snsapi_userinfo";
        req.scope = @"snsapi_message,snsapi_userinfo,snsapi_friend,snsapi_contact";

    //@"snsapi_message,snsapi_userinfo,snsapi_friend,snsapi_contact"
        
        [WXApi sendAuthReq:req viewController:self delegate:[WXApiManager sharedManager]];

    
    }else {

        // 没有安装客户端的提示  请先安装微信客户端
        NSLog(@"没有安装客户端");

    }

}

#pragma mark -微信授权获取用户个人信息（UnionID机制）
- (void)wechatLoginByRequestForUserInfo {

    NSString *accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:WXAccess_token];
    NSString *openID = [[NSUserDefaults standardUserDefaults] objectForKey:WXOpenID];
    NSString *userUrlStr = [NSString stringWithFormat:@"%@/userinfo?access_token=%@&openid=%@", WX_BASE_URL, accessToken, openID];
    // 请求用户数据
    [PPNetworkHelper POST:userUrlStr parameters:nil success:^(id responseObject) {

        NSLog(@"请求用户信息的response = %@", responseObject);

    } failure:^(NSError *error) {

        NSLog(@"获取用户信息时出错 = %@", error);

    }];

}

#pragma mark -WXApiManagerDelegate
- (void)managerDidRecvAuthResponse:(SendAuthResp *)response {
    
    // 登录界面消失
    [self dismissViewControllerAnimated:YES completion:nil];

    NSString *url = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code", WxAppID, WXAppSecret, response.code];
    
    NSLog(@"access_tokenGET:\n%@", url);
    
    // 请求成功之后获取用户信息
    [PPNetworkHelper POST:url parameters:nil success:^(id responseObject) {

        NSLog(@"请求access的response = %@", responseObject);
        NSDictionary *accessDict = [NSDictionary dictionaryWithDictionary:responseObject];
        NSString *accessToken = [accessDict objectForKey:@"access_token"];
        NSString *openID = [accessDict objectForKey:@"openid"];
        NSString *refreshToken = [accessDict objectForKey:@"refresh_token"];
        // 本地持久化，以便access_token的使用、刷新或者持续
        if (accessToken && ![accessToken isEqualToString:@""] && openID && ![openID isEqualToString:@""]) {
            [[NSUserDefaults standardUserDefaults] setObject:accessToken forKey:WXAccess_token];
            [[NSUserDefaults standardUserDefaults] setObject:openID forKey:WXOpenID];
            [[NSUserDefaults standardUserDefaults] setObject:refreshToken forKey:WXRefresh_token];
            [[NSUserDefaults standardUserDefaults] synchronize]; // 命令直接同步到文件里，来避免数据的丢失

            NSLog(@"数据赋值");

        }

        [self wechatLoginByRequestForUserInfo];

    } failure:^(NSError *error) {

        NSLog(@"access_tokenGetError = %@", error);

    }];
}

#pragma mark -淘宝授权登录
- (void)tbBtnClick:(UIButton *)btn {
    
    NSLog(@"淘宝授权登录");
    
    [[CommonAction sharedInstance] authWithViewController:self andItemDetailPage:nil];
    
}

#pragma mark -注册账号按钮点击事件
- (void)registerBtnClick:(UIButton *)btn {
    
    NSLog(@"注册账号按钮点击事件");
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    
    JTNavigationController *nav = [[JTNavigationController alloc] initWithRootViewController:registerVC];

    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark -忘记密码按钮点击事件
- (void)forgetPasswordBtnClick:(UIButton *)btn {
    
    NSLog(@"忘记密码按钮点击事件");
    
    ModifyLoginPasswordViewController *vc = [[ModifyLoginPasswordViewController alloc] initWithControllerType:UIViewControllerForgetPasswordType withPhoneNum:self.phoneTextField.text];
//    vc.phoneNumStr = self.phoneTextField.text;
    JTNavigationController *nav = [[JTNavigationController alloc] initWithRootViewController:vc];
    UIButton *backBtn = [[UIButton alloc] init];
    [backBtn setImage:[UIImage imageNamed:@"nav_go_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(maskOver) forControlEvents:UIControlEventTouchUpInside];
    vc.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    
    [self presentViewController:nav animated:YES completion:nil];
//    [self.navigationController pushViewController:vc animated:YES];
    
}
- (void)maskOver {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

#pragma mark -登录按钮点击事件
- (void)loginBtnClick:(UIButton *)btn {
    
    NSLog(@"登录按钮点击事件");
    
    if (self.phoneTextField.text.length <= 0) {
        
        NSLog(@"请输入手机号码");
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入手机号码"];
        return;
    }
    
    if (self.phoneTextField.text.length > 11) {
        
        NSLog(@"请输入正确手机号码");
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入正确手机号码"];
        return;
    }
    
    if (self.passwordTextField.text.length <= 0) {
        
        NSLog(@"请输入密码");
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入密码"];
        return;
        
    }
    
    NSString *passwordStr = [RSA encryptString:[self.passwordTextField.text md5String] publicKey:PublicKey];
    
    NSString *par = [NSString stringWithFormat:@"{\"user_mobile\":\"%@\",  \"password\" : \"%@\"}", [RSA encryptString:self.phoneTextField.text publicKey:PublicKey], passwordStr];
    
    [PPNetworkHelper POST:@"user/login/login" parameters:@{@"json" : par} success:^(id responseObject) {
        
        NSLog(@"%@",responseObject);
        
        if ([responseObject[@"code"] integerValue] == 1) {
            
            NSDictionary *dict = responseObject[@"data"];
            
            NSData *photo = [NSData base64DataFromString:[NSString stringWithFormat:@"%@",dict[@"user_photo"]]];
            
            UIImage *image = [UIImage imageWithData:photo];
            
            [CommonUserInfo sharedInstance].isLogin = YES;
            
            [CommonUserInfo sharedInstance].user_id =  [NSString stringWithFormat:@"%@",dict[@"user_id"]];
            [CommonUserInfo sharedInstance].phone = dict[@"user_mobile"];
//            [NSString stringWithFormat:@"%@",dict[@"user_mobile"]];
            [CommonUserInfo sharedInstance].is_setPayPassword = [dict[@"is_paypass"] boolValue];
            [CommonUserInfo sharedInstance].is_Bind = [dict[@"is_bank"] boolValue];
            [CommonUserInfo sharedInstance].VIPLevel = [NSString stringWithFormat:@"%@",dict[@"vip"]];
            [CommonUserInfo sharedInstance].beanNum = [NSString stringWithFormat:@"%@",dict[@"user_bean"]];
            [CommonUserInfo sharedInstance].photoUrl = [NSString stringWithFormat:@"%@",dict[@"user_photo"]];
            
//            [CommonUserInfo sharedInstance].iconIV = image;
            [CommonUserInfo sharedInstance].balance = [NSString stringWithFormat:@"%@",dict[@"user_money"]];
            [CommonUserInfo sharedInstance].nickName = [NSString stringWithFormat:@"%@",dict[@"user_name"]];
            
//            UIWindow *window = [UIApplication sharedApplication].keyWindow;
//            window.rootViewController = [BaseTabbarViewController sharedMainTabBarVC];
            [TalkingData onLogin:[CommonUserInfo sharedInstance].phone type:TDAccountTypeRegistered name:[CommonUserInfo sharedInstance].nickName];
            [self dismissViewControllerAnimated:YES completion:^{
                if (self.callBackBlock) {
                    self.callBackBlock();
                }
            }];
            
        }else {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
            
        }
        
    } failure:^(NSError *error) {
        
        if (error) {
            
            NSLog(@"%@", error);
            [[KYAlertView sharedInstance] showAlertViewWithMessage:Tip];
       
        }
        
    }];

}

#pragma mark -关闭按钮
- (void)closeBtnClick:(UIButton *)btn {
    
    NSLog(@"关闭界面");
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)createView {
    
    [self.view addSubview:self.contentScrollView];
    [self.contentScrollView addSubview:self.titileIV];
    
    [self.contentScrollView addSubview:self.desL];
    [self.contentScrollView addSubview:self.leftIV];
    [self.contentScrollView addSubview:self.rightIV];
    
    [self.contentScrollView addSubview:self.phoneIV];
    [self.contentScrollView addSubview:self.phoneL];
    [self.contentScrollView addSubview:self.phoneZoneL];
    [self.contentScrollView addSubview:self.phoneTextField];
    [self.contentScrollView addSubview:self.shortLineIV];
    [self.contentScrollView addSubview:self.middleLineIV];
    
    [self.contentScrollView addSubview:self.passwordIV];
    [self.contentScrollView addSubview:self.passwordL];
    [self.contentScrollView addSubview:self.passwordTextField];
    [self.contentScrollView addSubview:self.longLineIV];
    
    [self.contentScrollView addSubview:self.goReisterBtn];
    [self.contentScrollView addSubview:self.forgetPasswrodBtn];
    
    [self.contentScrollView addSubview:self.otherLoginL];
    [self.contentScrollView addSubview:self.otherLeftV];
    [self.contentScrollView addSubview:self.otherRightV];
//    [self.contentScrollView addSubview:self.wxBtn];
    [self.contentScrollView addSubview:self.tbBtn];
    
    [self.contentScrollView addSubview:self.loginBtn];
    
    
    __weak typeof(self) weakSelf = self;

    [self.contentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.view.mas_top);
        make.bottom.mas_equalTo(weakSelf.view.mas_bottom);
        
    }];
    
//    [self.titileIV mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.centerX.mas_equalTo(weakSelf.view.mas_centerX); make.top.mas_equalTo(weakSelf.contentScrollView.mas_top).with.offset(jisuan(80));
//        make.width.mas_equalTo(jisuan(252));
//        make.height.mas_equalTo(jisuan(80));
//
//    }];
//    [self.desL mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.top.mas_equalTo(weakSelf.contentScrollView.top).with.offset(jisuan(284));
//        make.centerX.mas_equalTo(weakSelf.view.mas_centerX);
//
//    }];
    
    [self.titileIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.view.mas_centerX); make.top.mas_equalTo(weakSelf.contentScrollView.mas_top).with.offset(jisuan(120));
        make.width.mas_equalTo(jisuan(252));
        make.height.mas_equalTo(jisuan(80));
        
    }];
    [self.desL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakSelf.contentScrollView.top).with.offset(jisuan(324));
        make.centerX.mas_equalTo(weakSelf.view.mas_centerX);
        
    }];

    
    [self.leftIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(weakSelf.desL.mas_left).with.offset(-jisuan(28));
        make.centerY.mas_equalTo(weakSelf.desL.mas_centerY);
        make.width.mas_equalTo(jisuan(32));
        make.height.mas_equalTo(jisuan(4));
    }];
   
    [self.rightIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.desL.mas_right).with.offset(jisuan(28));
        make.centerY.mas_equalTo(weakSelf.desL.mas_centerY);
        make.width.mas_equalTo(jisuan(32));
        make.height.mas_equalTo(jisuan(4));
        
    }];
    
    [self.phoneIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(120));
        make.top.mas_equalTo(weakSelf.desL.mas_bottom).with.offset(jisuan(100));
        make.width.mas_equalTo(jisuan(30));
        make.height.mas_equalTo(jisuan(30));
    }];
    [self.phoneL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.phoneIV.mas_right).with.offset(Margin);
        make.centerY.mas_equalTo(weakSelf.phoneIV.mas_centerY);
        
    }];
    
    [self.phoneZoneL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(144));
        make.top.mas_equalTo(weakSelf.phoneIV.mas_bottom).with.offset(jisuan(24));
        make.width.mas_equalTo(jisuan(60));
        make.height.mas_equalTo(jisuan(40));
    }];
    [self.phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.phoneZoneL.mas_right).with.offset(jisuan(64));
        make.centerY.mas_equalTo(weakSelf.phoneZoneL.mas_centerY);
        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(120));
        make.height.mas_equalTo(jisuan(34));
    }];
    
    [self.shortLineIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(120));
        make.top.mas_equalTo(weakSelf.phoneIV.mas_bottom).with.offset(jisuan(84));
        make.width.mas_equalTo(jisuan(104));
        make.height.mas_equalTo(jisuan(2));
    }];
    [self.middleLineIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.shortLineIV.mas_right).with.offset(jisuan(38));
        make.centerY.mas_equalTo(weakSelf.shortLineIV.mas_centerY);
        make.width.mas_equalTo(jisuan(374));
        make.height.mas_equalTo(jisuan(2));
    }];
    
    [self.passwordIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(120));
        make.top.mas_equalTo(weakSelf.shortLineIV.mas_bottom).with.offset(jisuan(58));
        make.width.mas_equalTo(jisuan(32));
        make.height.mas_equalTo(jisuan(40));
    }];
    [self.passwordL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.passwordIV.mas_right).with.offset(jisuan(18));
        make.centerY.mas_equalTo(weakSelf.passwordIV.mas_centerY);
    }];
    
    [self.passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(120));
        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(120));
        make.top.mas_equalTo(weakSelf.passwordIV.mas_bottom).with.offset(jisuan(30));
        make.height.mas_equalTo(jisuan(34));
    }];
    
    [self.longLineIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(120));
        make.top.mas_equalTo(weakSelf.passwordIV.mas_bottom).with.offset(jisuan(82));
        make.width.mas_equalTo(jisuan(514));
        make.height.mas_equalTo(jisuan(2));
    }];
    
    [self.goReisterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(120));
        make.top.mas_equalTo(weakSelf.longLineIV.mas_bottom).with.offset(jisuan(8));
        make.width.mas_equalTo(jisuan(100));
        make.height.mas_equalTo(jisuan(28));
    }];
    
    [self.forgetPasswrodBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(120));
        make.top.mas_equalTo(weakSelf.longLineIV.mas_bottom).with.offset(jisuan(8));
        make.width.mas_equalTo(jisuan(100));
        make.height.mas_equalTo(jisuan(28));
    }];
    
    [self.otherLoginL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.view.mas_centerX);
        make.top.mas_equalTo(weakSelf.goReisterBtn.mas_bottom).with.offset(jisuan(100));
    }];
    [self.otherLeftV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(weakSelf.otherLoginL.mas_left).with.offset(-jisuan(10));
        make.centerY.mas_equalTo(weakSelf.otherLoginL.mas_centerY);
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(122));
        make.height.mas_equalTo(jisuan(2));
        
        
    }];
    [self.otherRightV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.otherLoginL.mas_right).with.offset(jisuan(10));
        make.centerY.mas_equalTo(weakSelf.otherLoginL.mas_centerY);
        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(122));
        make.height.mas_equalTo(jisuan(2));
        
        
    }];
    
    [self.tbBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(256));
        make.centerX.mas_equalTo(weakSelf.view.mas_centerX);
        make.top.mas_equalTo(weakSelf.otherLoginL.mas_bottom).with.offset(jisuan(40));
        make.width.mas_equalTo(jisuan(80));
        make.height.mas_equalTo(jisuan(80));
    }];
//    [self.wxBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(256));
//        make.top.mas_equalTo(weakSelf.otherLoginL.mas_bottom).with.offset(jisuan(40));
//        make.width.mas_equalTo(jisuan(80));
//        make.height.mas_equalTo(jisuan(80));
//    }];
//    [self.tbBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(256));
//        make.top.mas_equalTo(weakSelf.otherLoginL.mas_bottom).with.offset(jisuan(40));
//        make.width.mas_equalTo(jisuan(80));
//        make.height.mas_equalTo(jisuan(80));
//    }];
    
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.view.mas_centerX);

        make.top.mas_equalTo(weakSelf.tbBtn.mas_bottom).with.offset(jisuan(100));
        make.width.mas_equalTo(jisuan(410));
        make.height.mas_equalTo(jisuan(106));
    }];
    
    [self.view layoutIfNeeded];
    
    self.contentScrollView.contentSize = CGSizeMake(WIDTH, CGRectGetMaxY(self.loginBtn.frame) + jisuan(106) + jisuan(100));

}


#pragma mark -lazy

- (UIScrollView *)contentScrollView {
    
    if (!_contentScrollView) {
        
        _contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
        
        _contentScrollView.showsVerticalScrollIndicator = NO;
        _contentScrollView.showsHorizontalScrollIndicator = NO;
        
        if (@available(iOS 11.0, *)) {
            
            _contentScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    
    return _contentScrollView;
}

- (UIButton *)closeBtn {
    
    if (!_closeBtn) {
        
        _closeBtn = [[UIButton alloc] init];
        [_closeBtn setImage:[UIImage imageNamed:@"coloseBG"] forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(closeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }

    return _closeBtn;
}


- (UIImageView *)titileIV {
    
    if (!_titileIV) {
        
        _titileIV = [[UIImageView alloc] init];
        _titileIV.image = [UIImage imageNamed:@"titleIvBG"];
        _titileIV.contentMode = UIViewContentModeScaleAspectFit;
        
    }
    
    return _titileIV;
}

- (UIImageView *)leftIV {
    
    if (!_leftIV) {
        
        _leftIV = [[UIImageView alloc] init];
        _leftIV.image = [UIImage imageNamed:@"leftIVBG"];
        _leftIV.contentMode = UIViewContentModeScaleAspectFit;
        
    }
    
    return _leftIV;
}

- (UIImageView *)rightIV {
    
    if (!_rightIV) {
        
        _rightIV = [[UIImageView alloc] init];
        _rightIV.image = [UIImage imageNamed:@"rightIVbg"];
        _rightIV.contentMode = UIViewContentModeScaleAspectFit;
        
    }
    
    return _rightIV;
}

- (UILabel *)desL {
    
    if (!_desL) {
        
        _desL = [[UILabel alloc] init];
        _desL.text = @"一款让你爱上购物的APP";
        _desL.textColor = Color333333;
        _desL.font = [UIFont fontWithName:@":MFYueYuan_Noncommercial-Regular" size:jisuan(32)];
        _desL.textAlignment = NSTextAlignmentCenter;
    }
    
    return _desL;
}

- (UIImageView *)phoneIV {
    
    if (!_phoneIV) {
        
        _phoneIV = [[UIImageView alloc] init];
        _phoneIV.contentMode = UIViewContentModeScaleAspectFit;
        _phoneIV.image = [UIImage imageNamed:@"phoneBG"];
        
    }
    
    return _phoneIV;
}

- (UILabel *)phoneL {
    
    if (!_phoneL) {
        
        _phoneL = [[UILabel alloc] init];
        _phoneL.text = @"手机";
        _phoneL.textColor = Color333333;
//        _phoneL.font = [UIFont fontWithName:@":PingFangSC-Regular" size:jisuan(24)];
        _phoneL.font = Font(24);
        _phoneL.textAlignment = NSTextAlignmentLeft;
    }
    
    return _phoneL;
    
}
- (UILabel *)phoneZoneL {
    
    if (!_phoneZoneL) {
        
        _phoneZoneL = [[UILabel alloc] init];
        _phoneZoneL.text = @"+86";
        _phoneZoneL.textColor = Color333333;
        _phoneZoneL.font = Font(24);
        _phoneZoneL.textAlignment = NSTextAlignmentLeft;
    }
    
    return _phoneZoneL;
    
}

- (UITextField *)phoneTextField {
    
    if (!_phoneTextField) {
        
        _phoneTextField = [[UITextField alloc] init];
        _phoneTextField.placeholder = @"请输入手机号";
        _phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
        _phoneTextField.textColor = Color333333;
        _phoneTextField.font = Font(24);
        _phoneTextField.textAlignment = NSTextAlignmentLeft;
        
    }
    
    return _phoneTextField;
    
}

- (UIView *)shortLineIV {
    
    if (!_shortLineIV) {
        
        _shortLineIV = [[UIView alloc] init];
        _shortLineIV.backgroundColor = Colordddddd;
        
    }
    
    return _shortLineIV;
}

- (UIView *)middleLineIV {
    
    if (!_middleLineIV) {
        
        _middleLineIV = [[UIView alloc] init];
        _middleLineIV.backgroundColor = Colordddddd;
        
    }
    
    return _middleLineIV;
}

- (UIImageView *)passwordIV {
    
    if (!_passwordIV) {
        
        _passwordIV = [[UIImageView alloc] init];
        _passwordIV.contentMode = UIViewContentModeScaleAspectFit;
        _passwordIV.image = [UIImage imageNamed:@"passwordBG"];
    }
    
    return _passwordIV;
}

- (UILabel *)passwordL {
    
    if (!_passwordL) {
        
        _passwordL = [[UILabel alloc] init];
        _passwordL.text = @"密码";
        _passwordL.textColor = Color333333;
        _passwordL.font = Font(24);
        _passwordL.textAlignment = NSTextAlignmentLeft;
    
    }
    
    return _passwordL;
    
}

- (UITextField *)passwordTextField {
    
    if (!_passwordTextField) {
        
        _passwordTextField = [[UITextField alloc] init];
        _passwordTextField.placeholder = @"请输入密码";
        _passwordTextField.keyboardType = UIKeyboardTypeDefault;
        _passwordTextField.textAlignment = NSTextAlignmentLeft;
        _passwordTextField.textColor = Color333333;
        _passwordTextField.font = Font(24);
        _passwordTextField.secureTextEntry = YES;
        
    }
    
    return _passwordTextField;
    
}

- (UIView *)longLineIV {
    
    if (!_longLineIV) {
        
        _longLineIV = [[UIView alloc] init];
        _longLineIV.backgroundColor = Colordddddd;

    }
    
    return _longLineIV;
}

- (UIView *)otherLeftV {
    
    if (!_otherLeftV) {
        
        _otherLeftV = [[UIView alloc] init];
        _otherLeftV.backgroundColor = Colordddddd;
    
    }
    
    return _otherLeftV;
}
- (UIView *)otherRightV {
    
    if (!_otherRightV) {
        
        _otherRightV = [[UIView alloc] init];
        _otherRightV.backgroundColor = Colordddddd;
    
    }
    
    return _otherRightV;
}

- (UILabel *)otherLoginL {
    
    if (!_otherLoginL) {

        _otherLoginL = [[UILabel alloc] init];
        _otherLoginL.text = @"其他登录方式";
        _otherLoginL.textAlignment = NSTextAlignmentCenter;
        _otherLoginL.font = Font(20);
        _otherLoginL.textColor = Color9b9b9b;
        
    }
    
    return _otherLoginL;
    
}

- (UIButton *)wxBtn {
    
    if (!_wxBtn) {
        
        _wxBtn = [[UIButton alloc] init];
        UIImage *image = [UIImage imageNamed:@"wxLoginBG"];
        CGFloat top = 0; // 顶端盖高度
        CGFloat bottom = 0 ; // 底端盖高度
        CGFloat left = 0; // 左端盖宽度
        CGFloat right = 0; // 右端盖宽度
        UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
        image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
        
//        [_wxBtn setImage:[UIImage imageNamed:@"wxLoginBG"] forState:UIControlStateNormal];
        [_wxBtn setBackgroundImage:image forState:UIControlStateNormal];
//        _wxBtn.titleLabel.font = Font(20);
        [_wxBtn addTarget:self action:@selector(wxBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    }
    
    return _wxBtn;
    
}

- (UIButton *)tbBtn {
    
    if (!_tbBtn) {
        
        _tbBtn = [[UIButton alloc] init];
        
        UIImage *image = [UIImage imageNamed:@"tbLoginBG"];
        CGFloat top = 0; // 顶端盖高度
        CGFloat bottom = 0 ; // 底端盖高度
        CGFloat left = 0; // 左端盖宽度
        CGFloat right = 0; // 右端盖宽度
        UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
        image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];

        [_tbBtn setBackgroundImage:image forState:UIControlStateNormal];
//        [_tbBtn setImage:[UIImage imageNamed:@"tbLoginBG"] forState:UIControlStateNormal];
//        _tbBtn.titleLabel.font = Font(20);
        [_tbBtn addTarget:self action:@selector(tbBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _tbBtn;
}

- (UIButton *)goReisterBtn {
    
    if (!_goReisterBtn) {
        
        _goReisterBtn = [[UIButton alloc] init];
        [_goReisterBtn setTitle:@"注册账号" forState:UIControlStateNormal];
        [_goReisterBtn setTitle:@"注册账号" forState:UIControlStateHighlighted];
        [_goReisterBtn setTitleColor:Colorff4200 forState:UIControlStateNormal];
        [_goReisterBtn setTitleColor:Colorff4200 forState:UIControlStateHighlighted];

        _goReisterBtn.titleLabel.font = Font(20);
        [_goReisterBtn addTarget:self action:@selector(registerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _goReisterBtn;
}

- (UIButton *)forgetPasswrodBtn {
    
    if (!_forgetPasswrodBtn) {
        
        _forgetPasswrodBtn = [[UIButton alloc] init];
        [_forgetPasswrodBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
        [_forgetPasswrodBtn setTitle:@"忘记密码?" forState:UIControlStateHighlighted];
        [_forgetPasswrodBtn setTitleColor:Colorff4200 forState:UIControlStateNormal];
        [_forgetPasswrodBtn setTitleColor:Colorff4200 forState:UIControlStateHighlighted];
        
        _forgetPasswrodBtn.titleLabel.font = Font(20);
        [_forgetPasswrodBtn addTarget:self action:@selector(forgetPasswordBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _forgetPasswrodBtn;
}

- (UIButton *)loginBtn {
    
    if (!_loginBtn) {
        
        _loginBtn = [[UIButton alloc] init];
        [_loginBtn setImage:[UIImage imageNamed:@"loginBtnBg"] forState:UIControlStateNormal];
        [_loginBtn setImage:[UIImage imageNamed:@"loginBtnBg"] forState:UIControlStateHighlighted];
        [_loginBtn setTitleColor:Colorff4200 forState:UIControlStateNormal];
        [_loginBtn setTitleColor:Colorff4200 forState:UIControlStateHighlighted];
        [_loginBtn addTarget:self action:@selector(loginBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    }
    
    return _loginBtn;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
